package org.etcsoft.examples.genealogy.view;

import static org.etcsoft.examples.genealogy.figures.PersonFigure.*;

import org.eclipse.draw2d.ChopboxAnchor;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LightweightSystem;
import org.eclipse.draw2d.PolygonShape;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.draw2d.RectangleFigure;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.etcsoft.examples.genealogy.figures.MarriageFigure;
import org.etcsoft.examples.genealogy.figures.NoteFigure;
import org.etcsoft.examples.genealogy.figures.PersonFigure;
import org.etcsoft.examples.genealogy.listener.FigureMover;


public class GenealogyView {
    public static void main(String[] args) {
        new GenealogyView().run();
    }

    private void run() {
        Shell shell = new Shell(new Display());
        shell.setSize(365, 280);
        shell.setText("Genealogy");
        shell.setLayout(new GridLayout());

        Canvas canvas = this.createDiagram(shell);
        canvas.setLayoutData(new GridData(GridData.FILL_BOTH));

        Display display = shell.getDisplay();
        shell.open();
        while (!shell.isDisposed()) {
            while (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    private Canvas createDiagram(Composite parent) {
        Figure root = new Figure();
        root.setFont(parent.getFont());

        XYLayout layout = new XYLayout();
        root.setLayoutManager(layout);

        IFigure andy = new PersonFigure("andy", MALE, 1922, 2002);
        andy.add(new NoteFigure("Andy was good man."));
        root.add(andy);
        layout.setConstraint(andy, new Rectangle(new Point(10, 10), andy.getPreferredSize()));

        IFigure betty = new PersonFigure("betty", FEMALE, 1924, 2006);
        betty.add(new NoteFigure("Betty was good name."));
        root.add(betty);
        layout.setConstraint(betty, new Rectangle(new Point(230, 10), betty.getPreferredSize()));

        IFigure carl = new PersonFigure("carl", MALE, 1947, -1);
        carl.add(new NoteFigure("Carl is good man."));
        root.add(carl);
        layout.setConstraint(carl, new Rectangle(new Point(120, 120), carl.getPreferredSize()));

        IFigure marriage = new MarriageFigure(1942);
        root.add(marriage);
        layout.setConstraint(marriage, new Rectangle(new Point(145, 35), marriage.getPreferredSize()));

        NoteFigure note = new NoteFigure("Smith Family");
        note.setFont(parent.getFont());
        final Dimension noteSize = note.getPreferredSize();

        root.add(note, new Rectangle(new Point(10, 220 - noteSize.height), noteSize));

        root.add(this.connect(andy, marriage));
        root.add(this.connect(betty, marriage));
        root.add(this.connect(carl, marriage));

        Canvas canvas = new Canvas(parent, SWT.DOUBLE_BUFFERED);
        canvas.setBackground(ColorConstants.white);
        LightweightSystem lws = new LightweightSystem(canvas);
        lws.setContents(root);

        return canvas;
    }

    private IFigure connect(IFigure sourceFigure, IFigure targetFigure) {
        PolylineConnection connection = new PolylineConnection();
        connection.setSourceAnchor(new ChopboxAnchor(sourceFigure));
        connection.setTargetAnchor(new ChopboxAnchor(targetFigure));

        return connection;
    }

    private IFigure createMarriageFigure() {
        Rectangle r = new Rectangle(0, 0, 50, 50);
        PolygonShape polygonShape = new PolygonShape();
        polygonShape.setStart(r.getTop());
        polygonShape.addPoint(r.getTop());
        polygonShape.addPoint(r.getLeft());
        polygonShape.addPoint(r.getBottom());
        polygonShape.addPoint(r.getRight());
        polygonShape.addPoint(r.getTop());
        polygonShape.setEnd(r.getTop());
        polygonShape.setFill(true);
        polygonShape.setBackgroundColor(ColorConstants.lightGray);
        polygonShape.setPreferredSize(r.getSize());

        new FigureMover(polygonShape);

        return polygonShape;
    }

    private IFigure createPersonFigure(String name) {
        RectangleFigure rectangleFigure = new RectangleFigure();
        rectangleFigure.setBackgroundColor(ColorConstants.lightGray);
        rectangleFigure.setLayoutManager(new ToolbarLayout());
        rectangleFigure.setPreferredSize(100, 100);
        rectangleFigure.add(new Label(name));

        new FigureMover(rectangleFigure);

        return rectangleFigure;
    }
}
